package com.attendance.www.hr.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.PunchTimeBean;
import com.attendance.www.hr.entitys.PunchTime;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IPunchTimeService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 考勤设置
 * 
 * @version 2017/9/27-2017/9/28
 * @author 徐伟
 *
 */
@Controller
@RequestMapping("/punchTime")
public class PunchTimeController {

	@Resource
	private IPunchTimeService punchTimeService;
	
	
	/**
	 * 添加考勤设置接口
	 * 
	 * @param request
	 * @param punchTimeBean
	 * @return
	 */
	@RequestMapping(value = "/savePunchTime.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object savePunchTime(HttpServletRequest request, @RequestBody PunchTimeBean punchTimeBean) {
	  if(punchTimeService.getSavePunchTimeNameValidate(punchTimeBean)){	
		if (punchTimeBean != null) {
			if (StringUtils.isBlank(punchTimeBean.getPunchTimeName())
					||StringUtils.isBlank(punchTimeBean.getStartFromTime())
					|| StringUtils.isBlank(punchTimeBean.getStartToTime())
					|| StringUtils.isBlank(punchTimeBean.getEndFromTime())
					|| StringUtils.isBlank(punchTimeBean.getEndToTime()
							) || punchTimeBean.getIsDefault() == null) {
				throw new ResultFaultException("0_00001");
			}
			if (StringUtils.isEmpty(punchTimeBean.getRestFromTime())) {
				punchTimeBean.setRestFromTime(null);
			}
			if (StringUtils.isEmpty(punchTimeBean.getRestToTime())) {
				punchTimeBean.setRestToTime(null);
			}
			if (punchTimeService.getPunchTimeByPunchTime(punchTimeBean)) {
				throw new ResultFaultException("0_00010");
			} else {
				if (punchTimeService.savePunchTime(punchTimeBean)) {
					return ResultMap.success();
				} else {
					throw new ResultFaultException("0_00007");
				}
			}
		}
	}
		throw new ResultFaultException("0_00001");
	}

	/**
	 * 获取考勤设置接口
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getPunchTimeList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getPunchTimeList(HttpServletRequest request,@RequestBody PunchTimeBean punchTimeBean) {
		return ResultMap.success(punchTimeService.getPunchTimeList(punchTimeBean));
	}

	/**
	 * 修改考勤设置接口
	 * 
	 * @param request
	 * @param punchTimeBean
	 * @return
	 */
	@RequestMapping(value = "/updatePunchTime.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updatePunchTimeList(HttpServletRequest request, @RequestBody PunchTimeBean punchTimeBean) {
		if(punchTimeService.getUpdatePunchTimeNameValidate(punchTimeBean)){
		if (punchTimeBean != null) {
			if (StringUtils.isBlank(punchTimeBean.getPunchTimeName())
					|| StringUtils.isBlank(punchTimeBean.getStartFromTime())
					|| StringUtils.isBlank(punchTimeBean.getStartToTime())
					|| StringUtils.isBlank(punchTimeBean.getEndFromTime())
					|| StringUtils.isBlank(punchTimeBean.getEndToTime()) || punchTimeBean.getIsDefault() == null) {
				throw new ResultFaultException("0_00001");
			}
			PunchTime punchTime = punchTimeService.getPunchTimeByPunchTimeId(punchTimeBean.getPunchTimeId());
			if (punchTime != null) {
				if (punchTime.getUpdateTime().compareTo(punchTimeBean.getUpdateTime()) == 0) {
					if (punchTimeService.getPunchTimeByPunchTime(punchTimeBean)) {
						throw new ResultFaultException("0_00010");
					} else {
						if (punchTimeService.updatePunchTime(punchTimeBean)) {
							return ResultMap.success();
						} else {
							throw new ResultFaultException("0_00007");
						}
					}
				} else {
					throw new ResultFaultException("0_00011");
				}
			} else {
				throw new ResultFaultException("0_00012");
			}
		}
		}
		throw new ResultFaultException("0_00001");
	}

	/**
	 * 删除考勤设置接口
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deletePunchTime.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deletePunchTime(HttpServletRequest request) {
		String punchTimeIdString = request.getParameter("punchTimeId");
		if (StringUtils.isBlank(punchTimeIdString)) {
			throw new ResultFaultException("0_00001");
		}

		if(punchTimeService.getDeletePunchTimeNameValidate(punchTimeIdString)){
				Integer punchTimeId = Integer.valueOf(punchTimeIdString);
				if(punchTimeService.deletePunchTimeList(punchTimeId)){
					return ResultMap.success(punchTimeService.deletePunchTimeList(punchTimeId));
				}
		}
		throw new ResultFaultException("0_00009");				
	}
}
